// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Fireball Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 56 : TYPE_FIREBALL

// Function declarations
reserve function Fireball_DebugDraw
reserve function Fireball_DebugSpawn

// Static Values
public value Fireball_value34 = 0;

// Tables

function Fireball_DebugDraw
	temp0 = object[0].value17
	temp0 -= Fireball_value34
	temp1 = object.direction
	switch temp0
	case 0
		object.direction = FLIP_Y
		DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
		break
	case 1
		if object.direction == FLIP_NONE
			object.direction = FLIP_Y
		end if
		DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		DrawSpriteFX(3, FX_FLIP, object.xpos, object.ypos)
		break
	end switch
	object.direction = temp1
end function


function Fireball_DebugSpawn
	CreateTempObject(TypeName[Fireball], 0, object.xpos, object.ypos)
	object[tempObjectPos].value1 = 60
	temp0 = object[0].value17
	temp0 -= Fireball_value34
	switch temp0
	case 0
		object[tempObjectPos].propertyValue = 1
		object[tempObjectPos].yvel = -0x40000
		object[tempObjectPos].value2 = 2
		break
	case 1
		if object.direction == FLIP_NONE
			object[tempObjectPos].propertyValue = 2
			object[tempObjectPos].yvel = -0x20000
			object[tempObjectPos].value2 = 2
		else
			object[tempObjectPos].propertyValue = 3
			object[tempObjectPos].yvel = 0x20000
			object[tempObjectPos].value2 = 0
		end if
		break
	case 2
		if object.direction == FLIP_NONE
			object[tempObjectPos].propertyValue = 5
			object[tempObjectPos].xvel = 0x20000
			object[tempObjectPos].value2 = 0
		else
			object[tempObjectPos].propertyValue = 4
			object[tempObjectPos].xvel = -0x20000
			object[tempObjectPos].value2 = 1
		end if
		break
	end switch
end function


event ObjectMain
	switch object.state
	case 0
		object.value0++
		if object.value0 == object.value1
			object.value0 = 0
			temp0 = object.ixpos
			temp0 -= screen.cameraX
			Abs(temp0)
			if temp0 < screen.xcenter
				temp0 = object.iypos
				temp0 -= screen.cameraY
				Abs(temp0)
				if temp0 < screen.ycenter
					CreateTempObject(TypeName[Fireball], 0, object.xpos, object.ypos)
					object[tempObjectPos].state = object.propertyValue
					object[tempObjectPos].xvel = object.xvel
					object[tempObjectPos].yvel = object.yvel
					object[tempObjectPos].value3 = object.ypos
					object[tempObjectPos].value2 = object.value2
					PlaySfx(SfxName[Small Fireball], 0)
				end if
			end if
		end if
		break
	case 1
		object.yvel += 0x1800
		object.ypos += object.yvel
		if object.yvel > 0
			object.value2 = 0
		end if
		if object.ypos > object.value3
			object.type = TypeName[Blank Object]
		end if
		object.frame = object.animationTimer
		object.frame /= 12
		object.direction = object.animationTimer
		object.direction /= 6
		object.direction &= FLIP_X
		object.direction += object.value2
		object.animationTimer++
		object.animationTimer %= 24
		break
	case 2
		object.ypos += object.yvel
		object.frame = object.animationTimer
		object.frame /= 12
		object.direction = object.animationTimer
		object.direction /= 6
		object.direction &= FLIP_X
		object.direction += object.value2
		object.animationTimer++
		object.animationTimer %= 24
		ObjectTileCollision(CSIDE_ROOF, 0, -8, 0)
		if checkResult == 1
			object.state = 6
			object.frame = 2
		end if
		if object.outOfBounds == 1
			object.type = TypeName[Blank Object]
		end if
		break
	case 3
		object.ypos += object.yvel
		object.frame = object.animationTimer
		object.frame /= 12
		object.direction = object.animationTimer
		object.direction /= 6
		object.direction &= FLIP_X
		object.direction += object.value2
		object.animationTimer++
		object.animationTimer %= 24
		ObjectTileCollision(CSIDE_FLOOR, 0, 8, 0)
		if checkResult == 1
			object.state = 6
			object.frame = 2
		end if
		if object.outOfBounds == 1
			object.type = TypeName[Blank Object]
		end if
		break
	case 4
		object.xpos += object.xvel
		object.frame = object.animationTimer
		object.frame /= 12
		object.frame += 3
		object.direction = object.animationTimer
		object.direction /= 6
		object.direction &= FLIP_X
		object.direction <<= FLIP_X
		object.direction += object.value2
		object.animationTimer++
		object.animationTimer %= 24
		ObjectTileCollision(CSIDE_RWALL, -8, 0, 0)
		if checkResult == 1
			object.state = 6
			object.frame = 5
		end if
		if object.outOfBounds == 1
			object.type = TypeName[Blank Object]
		end if
		break
	case 5
		object.xpos += object.xvel
		object.frame = object.animationTimer
		object.frame /= 12
		object.frame += 3
		object.direction = object.animationTimer
		object.direction /= 6
		object.direction &= FLIP_X
		object.direction <<= FLIP_X
		object.direction += object.value2
		object.animationTimer++
		object.animationTimer %= 24
		ObjectTileCollision(CSIDE_LWALL, 8, 0, 0)
		if checkResult == 1
			object.state = 6
			object.frame = 5
		end if
		if object.outOfBounds == 1
			object.type = TypeName[Blank Object]
		end if
		break
	case 6
		object.value0++
		if object.value0 == 8
			object.type = TypeName[Blank Object]
		end if
		break
	end switch
	if object.state > 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -6, -6, 6, 6, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				CallFunction(PlayerObject_FireHit)
			end if
		next
	end if
end event


event ObjectDraw
	if object.state > 0
		DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
	end if
end event


event ObjectStartup
	CheckCurrentStageFolder("BossRush")
	if checkResult == 1
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-7, -23, 15, 31, 158, 429)
		SpriteFrame(-8, -24, 16, 32, 174, 429)
		SpriteFrame(-8, -19, 16, 23, 191, 429)
		SpriteFrame(-23, -8, 31, 15, 209, 429)
		SpriteFrame(-24, -8, 32, 15, 208, 445)
		SpriteFrame(-15, -8, 23, 16, 241, 429)
	else
		LoadSpriteSheet("MZ/Objects.gif")
		SpriteFrame(-7, -23, 15, 31, 52, 114)
		SpriteFrame(-8, -24, 16, 32, 68, 114)
		SpriteFrame(-8, -15, 16, 23, 85, 114)
		SpriteFrame(-23, -8, 31, 15, 103, 114)
		SpriteFrame(-24, -8, 32, 15, 102, 130)
		SpriteFrame(-15, -8, 23, 16, 135, 114)
	end if
	foreach (TypeName[Fireball], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value1 = object[arrayPos0].propertyValue
		object[arrayPos0].value1 >>= 4
		object[arrayPos0].value1 *= 30
		object[arrayPos0].value1 += 30
		object[arrayPos0].propertyValue &= 7
		switch object[arrayPos0].propertyValue
		case 0
			object[arrayPos0].propertyValue = 1
			object[arrayPos0].yvel = -0x40000
			object[arrayPos0].value2 = 2
			break
		case 1
			object[arrayPos0].propertyValue = 1
			object[arrayPos0].yvel = -0x50000
			object[arrayPos0].value2 = 2
			break
		case 2
			object[arrayPos0].propertyValue = 1
			object[arrayPos0].yvel = -0x60000
			object[arrayPos0].value2 = 2
			break
		case 3
			object[arrayPos0].propertyValue = 1
			object[arrayPos0].yvel = -0x70000
			object[arrayPos0].value2 = 2
			break
		case 4
			object[arrayPos0].propertyValue = 2
			object[arrayPos0].yvel = -0x20000
			object[arrayPos0].value2 = 2
			break
		case 5
			object[arrayPos0].propertyValue = 3
			object[arrayPos0].yvel = 0x20000
			object[arrayPos0].value2 = 0
			break
		case 6
			object[arrayPos0].propertyValue = 4
			object[arrayPos0].xvel = -0x20000
			object[arrayPos0].value2 = 1
			break
		case 7
			object[arrayPos0].propertyValue = 5
			object[arrayPos0].xvel = 0x20000
			object[arrayPos0].value2 = 0
			break
		end switch
	next
	temp0 = 0
	Fireball_value34 = DebugMode_ObjCount
	while temp0 < 3
		SetTableValue(TypeName[Fireball], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(Fireball_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(Fireball_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
